package File;

import java.io.*;
import java.util.Scanner;

/**
 * 实现普通文件的拷贝
 */
public class TestDemo5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要复制的文件路径：");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);
        if (!srcFile.exists()) {
            System.out.println("要复制的文件不存在！");
            return;
        }
        if (!srcFile.isFile()) {
            System.out.println("要复制的不是一个普通文件！");
            return;
        }
        System.out.println("请输入您要复制到哪个路径：");
        String descPath = scanner.next();
        File descFile = new File(descPath);
        if (descFile.exists()) {
            System.out.println("该路径文件已经存在了，是否要覆盖(Y/N)");
            String choice = scanner.next();
            if (choice.equals('Y')) {
                descFile.delete();
                System.out.println("覆盖成功");
            } else {
                System.out.println("取消覆盖");
                return;
            }
        }
        System.out.println("-----开始执行拷贝操作-----");
        try (InputStream inputStream = new FileInputStream(srcFile)) {
            try (OutputStream outputStream = new FileOutputStream(descFile)) {
                byte[] buffer = new byte[1024];//缓冲区
                while (true) {
                    int len = inputStream.read(buffer);
                    if (len == -1) {
                        break;
                    }
                    outputStream.write(buffer, 0, len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("拷贝完成");
    }
}
